package com.shop.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.diagnosis.DiagnosisUtils;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.BusinessParams;
import com.alipay.api.request.AlipayTradePrecreateRequest;

import com.alipay.api.FileItem;
import com.shop.bean.Orders;
import com.shop.bean.Orders;

import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class PaymentUtil {

    public static String payment(Orders order) throws AlipayApiException {
        System.out.println("刚刚进入支付函数！");
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        System.out.println("设置参数完成");

        // 构造请求参数以调用接口
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();

//            // 设置商户订单号
        //model.setOutTradeNo("20150320010101001");
        model.setOutTradeNo(order.getCode());
//
//            // 设置订单总金额
        //model.setTotalAmount("88.88");
        model.setTotalAmount(order.getMoney().toString());
//
//            // 设置订单标题
        model.setSubject("鞋梦弄潮商城");
//
//            // 设置产品码
        model.setProductCode("FACE_TO_FACE_PAYMENT");

        // 设置卖家支付宝用户ID
        model.setSellerId("2088721061035351");

        // 设置订单附加信息
//            model.setBody("Iphone6 16G");
//
//            // 设置订单包含的商品列表信息
//            List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
//
//            GoodsDetail goodsDetail0 = new GoodsDetail();
//            goodsDetail0.setGoodsName("ipad");
//            goodsDetail0.setQuantity(1L);
//            goodsDetail0.setPrice("2000");
//            goodsDetail0.setGoodsId("apple-01");
//            goodsDetail0.setGoodsCategory("34543238");
//            goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
//            goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
//            goodsDetail.add(goodsDetail0);
//            model.setGoodsDetail(goodsDetail);

//            // 设置业务扩展参数
//            ExtendParams extendParams = new ExtendParams();
//            extendParams.setSysServiceProviderId("2088511833207846");
//            model.setExtendParams(extendParams);

        // 设置商户传入业务信息
//            BusinessParams businessParams = new BusinessParams();
//            businessParams.setMcCreateTradeIp("127.0.0.1");
//            model.setBusinessParams(businessParams);

        // 设置可打折金额
//            model.setDiscountableAmount("80.00");

        // 设置商户门店编号
//            model.setStoreId("NJ_001");

        // 设置商户操作员编号
//            model.setOperatorId("yx_001");

        // 设置商户机具终端编号
//            model.setTerminalId("NJ_T_001");

        // 设置商户的原始订单号
//            model.setMerchantOrderNo("20161008001");

        request.setBizModel(model);
        request.setNotifyUrl("http://7a11f68d.r20.vip.cpolar.cn/orders/paymentsuccess");
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");


        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        System.out.println(response);
        System.out.println(response.getBody());


        if (response.isSuccess()) {
            String body=response.getBody();
            JSONObject obj=JSON.parseObject(body);
            obj= obj.getJSONObject("alipay_trade_precreate_response");
            String qr_code=obj.getString("qr_code");
            System.out.println(qr_code);
            System.out.println("调用成功");
            return qr_code;
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            System.out.println(diagnosisUrl);
            throw new RuntimeException();
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCcLPP/P0GBsUB8F1KEDCXOMNpWt5Pg9e/fs967f41tE38IS98BTJWsrP8E4rP9duE/I50/hMLwlA6mOcIHPVNlkcD+QCyc2AJ00HdzCXrzfe18JpysHqjQ4y7P1Czun/4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LPj4g4bAoGAAyquC3HZLQbIcEPHvCNmjwGsr4D8Mw1B8CUD7W5X3qheJW5oZIRpSodTV4MyjRd9L2uuxJ5Mlool7t9fWhG8LJ0rA45gtpkYY3Q/r0tUJ11rm0thThAuBmC+MYHOGGAscbr/cz3eQ+MKJ//XW6ZaL//ralpI+CwJQbAr79DZgV0CgYEAiS/Bz88fXXKGSGDF35nmd9OhcRwna/wx0+7CcQptuyO+uQDQLqfGjx9wCInfrbSsosvdEn2QOmiBPTulaI5a/aXI48VzCh2GpI2gX70QmrlqS7Kpjo32OrXRkNxF00NVU1aP8GlibyW+D/Vyornc7LmX9fK5lmhgt46al4wsQyE=";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw3VYc28/FsJ3e3T/pwhxOh8nBXnHIcj61khS6K07Nzqvs85TmukYzFVr6WHdWywq7o5iFdfFvZHMNnHkB1HWOTPBPB36QOkTHH4OSq5a+xkLFjj5FAL1Mk93irFV/rYwaNAA7MhYhYFrSme+L+2MSrOmjReXHPxHjSHzpy/QxBWM/IZxrUVydgf7uAScvhC7qLcpRji75MyW0HsDOCjVUWoBU7ya9tXFlkCX0yggjUzC1K8CjCljIf2flDgy+AZRK4VZT2fUyLBjvbYtHU/Nvi33UglhfsL5/kSxuJftMv2O3BmsuUQLc3gg2YPa688q9A+4efSz2omT0D1ccLfl7wIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000146601336");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        System.out.println("完成设置参数");
        return alipayConfig;
    }
}

